﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors.Repository;
using System.Windows.Forms;
using Model;
using DevExpress.Utils;
using DevExpress.XtraEditors;

namespace ServiceCenterApp.Helper
{
    public class WinformHelper
    {
        static Form owner = null;

        public static Form MainWindow
        {
            get { return WinformHelper.owner; }
            set { WinformHelper.owner = value; }
        }

        public static void BindLookUpEdit<T>(RepositoryItemLookUpEdit lue, List<T> list, string displayMember, string valueMember)
        {
            lue.DataSource = list;
            lue.ValueMember = valueMember;
            lue.DisplayMember = displayMember;
        }

        public static void BindRepository<T>(RepositoryItemCheckedComboBoxEdit repositoryItemCheckedComboBoxEdit1, List<T> list, string displayMember, string valueMember)
        {
            repositoryItemCheckedComboBoxEdit1.DataSource = list;
            repositoryItemCheckedComboBoxEdit1.ValueMember = valueMember;
            repositoryItemCheckedComboBoxEdit1.DisplayMember = displayMember;
        }

        public static TreeNode SearchNode(TreeNodeCollection collection, long id)
        {
            TreeNode result = null;
            foreach (TreeNode node in collection)
            {
                result = Find(node, id);
                if (result != null)
                {
                    break;
                }
            }
            return result;
        }

        public static void ShowInformation(string message)
        {
            XtraMessageBox.Show(owner,message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public static void ShowError(string message)
        {
            XtraMessageBox.Show(owner, message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private static TreeNode Find(TreeNode node, long id)
        {
            if (((Category)node.Tag).ID == id) { return node; }
            TreeNode searchedNode = null;
            foreach (TreeNode t in node.Nodes)
            {
                searchedNode = Find(t, id);
                if (searchedNode != null) { break; }
            }
            return searchedNode;
        }

        private static void ShowToolTip(ToolTipController controller, Control control, ToolTipIconType type, string title, string message)
        {
            ToolTipControllerShowEventArgs tool = new ToolTipControllerShowEventArgs();
            tool.IconType = type;
            tool.SelectedControl = control;
            tool.Title = title;
            tool.ToolTip = message;
            tool.ToolTipLocation = ToolTipLocation.TopRight;
            controller.ShowHint(tool, control);
        }
    }
}
